package com.weishi.project.training.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.enums.TrainingProgramEnum;
import com.weishi.common.utils.StringUtils;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * TrainingAchievementController
 */
@Api(value = "训练成绩",description = "训练成绩")
@RestController
@RequestMapping("/trainingAchievement/trainingAchievement")
public class TrainingAchievementController extends BaseController {

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     * 测功仪数据 接口
     */
    @Autowired
    private IDynamometerService dynamometerService;

    /**
     * 赛后数据表接口
     */
    @Autowired
    private IRaceDataService raceDataService;

    /**
     * 分段成绩表 接口
     */
    @Autowired
    private IDynamometerSubsectionService dynamometerSubsectionService;

    /**
     * 运动员 接口
     */
    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 船速仪 接口
     */
    @Autowired
    private IShipSpeedService shipSpeedService;

    /**
     * 水上分段 接口
     */
    @Autowired
    private IHeartRateCountService heartRateCountService;

    /**
     * 水上分段心率接口
     */
    @Autowired
    private IHeartRateRecordService heartRateRecordService;

    /**
     * 课程接口
     */
    @Autowired
    private ITrainingDayService trainingDayService;



    /**
     * 查询训练成绩列表
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingAchievement trainingAchievement) {
        if(trainingAchievement.getRowingKilometersTwo() == null){
            trainingAchievement.setRowingKilometers(0);
        }else{
            trainingAchievement.setRowingKilometers(trainingAchievement.getRowingKilometersTwo());
        }
        startPage();
        List<TrainingAchievement> list = trainingAchievementService.selectTrainingAchievementList(trainingAchievement);

        // 查询所有运动员
        List<Sportsman> sportsmanList = sportsmanService.list();

        // 判断
        if(list.size() > 0){
            list.forEach( p-> {

                // 陆上成绩
                if(p.getFirstLevel().equals("land_specific_training_dynamometer")){

                    // 设置存储运动员 id 的容器
                    List<TrainingAchievement> trainingAchievementList = trainingAchievementService
                            .list(Wrappers.<TrainingAchievement>lambdaQuery()
                                    .eq(TrainingAchievement::getChannelId , p.getId()));
                    // 设置存储运动员 名称 的容器
                    String sportsmanName = "";
                    for(int j = 0; j<trainingAchievementList.size(); j++){
                        for (int g = 0; g<sportsmanList.size(); g++){
                            if(trainingAchievementList.get(j).getSportsmanId().equals(sportsmanList.get(g).getId())) {
                                sportsmanName+=sportsmanList.get(g).getName() + " ";
                            }
                        }
                    }

                    // 存入 运动员名称
                    if(StringUtils.isNotNull(sportsmanName) && !"".equals(sportsmanName)){
                        p.setSportsmanName(sportsmanName.substring(0,sportsmanName.length()-1));
                    }


                    List<Dynamometer> dynamometerList = dynamometerService
                            .list(Wrappers.<Dynamometer>lambdaQuery()
                                    .eq(Dynamometer::getSportsmanId , p.getSportsmanId())
                                    .eq(Dynamometer::getGroupId , p.getGroupId())
                                    .eq(Dynamometer::getTrainingMode , p.getTrainingMode())
                                    .orderByDesc(Dynamometer::getHeartRate));

                    if(dynamometerList.size() > 0){
                        p.setMaxHeartRate(dynamometerList.get(0).getHeartRate());
                    }

                    // 判断最大心率如果是null 就取运动员成绩表中的最大心率
                    if(StringUtils.isNotNull(p.getMaxHeartRate())){
                        p.setMaxHeartRate(p.getMaxHeartRate());
                    }

                    // 查询其它心率
                    List<RaceData> raceDataList = raceDataService
                            .list(Wrappers.<RaceData>lambdaQuery()
                                    .eq(RaceData::getSportsmanId , p.getSportsmanId())
                                    .eq(RaceData::getTrainingDayId , p.getTrainingDayId()));

                    // 其它心率
                    String otherHeartRate = "";
                    // 其它乳酸
                    String otherLacticAcid = "";
                    if(raceDataList.size() > 0){
                        for (int i = 0; i<raceDataList.size(); i++){
                            // 判断其它心率
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getHeartRate())){
                                otherHeartRate += raceDataList.get(i).getHeartRateTitle()+":"+raceDataList.get(i).getHeartRate() + " ";
                            }
                            // 判断其它乳酸
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getLacticAcid())){
                                otherLacticAcid += raceDataList.get(i).getLacticAcidTitle()+":"+raceDataList.get(i).getLacticAcid() + " ";
                            }
                        }
                    }
                    // 存入其它心率
                    if(otherHeartRate.length() > 0){
                        String otherHeartRates = otherHeartRate.substring(0 , otherHeartRate.length()-1);
                        p.setOtherHeartRate(otherHeartRates);
                    }
                    // 存入其它乳酸
                    if(otherLacticAcid.length() > 0){
                        String otherLacticAcids = otherLacticAcid.substring(0 , otherLacticAcid.length()-1);
                        p.setOtherLacticAcid(otherLacticAcids);
                    }

                        // 查询分段成绩
                        List<DynamometerSubsection> dynamometerSubsectionList = dynamometerSubsectionService
                                .list(Wrappers.<DynamometerSubsection>lambdaQuery()
                                        .eq(DynamometerSubsection::getSportsmanId , p.getSportsmanId())
                                        .eq(DynamometerSubsection::getGroupId , p.getGroupId()));

                        // 分段成绩
                        String averageAchievement = "";
                        // 取最大心率 计算平均心率
                        Integer averageHeartRate = 0;
                        // 平均功率
                        Integer averagePower = 0;
                        // 平均桨频
                        Double averagePropellerFrequency = 0.0;
                        // 分段心率
                        String segmentHeartRate = "";
                        // 分段桨频
                        String segmentPropellerFrequency = "";
                        // 分段功率
                        String segmentPower = "";
                        // 分段瓦特
                        String segmentWatt = "";
                        // 平均瓦特
                        Integer averageWatt = 0;
                        if(dynamometerSubsectionList.size() > 0){
                            for(int i = 0; i<dynamometerSubsectionList.size() ;i++){
                                if(!"".equals(dynamometerSubsectionList.get(i).getWatt()) && StringUtils.isNotNull(dynamometerSubsectionList.get(i).getWatt())){
                                    averageWatt += Integer.parseInt(dynamometerSubsectionList.get(i).getWatt());
                                }
                                if(dynamometerSubsectionList.get(i).getTrainingMode() == 0){
                                    averageAchievement += dynamometerSubsectionList.get(i).getLengthTime() + " ";

                                }else{
                                    averageAchievement += dynamometerSubsectionList.get(i).getMileage() + " ";
                                }
                                //averageSpeed += dynamometerSubsectionList.get(i).getSpeed();
                                if(StringUtils.isNotNull(dynamometerSubsectionList.get(i).getHeartRate())){
                                    averageHeartRate+=dynamometerSubsectionList.get(i).getHeartRate();
                                }
                                // 存入桨数
                                //p.setPulpNumber(dynamometerSubsectionList.get(i).getPulpNumber());
                                p.setPower(dynamometerSubsectionList.get(0).getPower());
                                // 计算平均功率
                                averagePower += (int)StringUtils.turnSecond(dynamometerSubsectionList.get(i).getPower());
                                // 存入桨频
                                p.setPropellerFrequency(dynamometerSubsectionList.get(i).getPropellerFrequency());
                                // 计算平均桨频
                                averagePropellerFrequency += Double.parseDouble(dynamometerSubsectionList.get(i).getPropellerFrequency());
                                // 峰力
                                p.setPeakForce(dynamometerSubsectionList.get(i).getPeakForce());
                                // 均力
                                p.setAverageForce(dynamometerSubsectionList.get(i).getAverageForce());
                                // 平均瓦特
                                p.setAverageWatt(averageWatt / dynamometerSubsectionList.size());
                                // 分段心率
                                segmentHeartRate += dynamometerSubsectionList.get(i).getHeartRate() + " ";
                                // 分段桨频
                                segmentPropellerFrequency += dynamometerSubsectionList.get(i).getPropellerFrequency() + " ";
                                // 分段功率
                                segmentPower += dynamometerSubsectionList.get(i).getPower() + " ";
                                // 分段瓦特
                                segmentWatt += dynamometerSubsectionList.get(i).getWatt() + " ";
                            }
                            // 存储分段成绩
                            String segmentedTiming = averageAchievement.substring(0 , averageAchievement.length()-1);
                            p.setSegmentedTiming(segmentedTiming);
                            // 存储平均心率
                            p.setAverageHeartRate(averageHeartRate / dynamometerSubsectionList.size());
                            // 存储平均功率
                            Integer averagePowers = averagePower/dynamometerSubsectionList.size();
                            p.setAveragePower(StringUtils.millisecondRotationTime(averagePowers));
                            // 平均桨频
                            p.setAveragePropellerFrequency(averagePropellerFrequency/dynamometerSubsectionList.size());
                            // 存储分段心率
                            p.setSegmentHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                            // 存储分段桨频
                            p.setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0 , segmentPropellerFrequency.length()-1));
                            // 存储分段功率
                            p.setSegmentPower(segmentPower.substring(0 , segmentPower.length()-1));
                            // 存储分段瓦特
                            p.setSegmentWatt(segmentWatt.substring(0 , segmentWatt.length()-1));
                        }


                    // 水上成绩
                }else if(p.getFirstLevel().equals("special_training_water")){


                    // 查询其它心率
                    List<RaceData> raceDataList = raceDataService
                            .list(Wrappers.<RaceData>lambdaQuery()
                                    .eq(RaceData::getSportsmanId , p.getSportsmanId())
                                    .eq(RaceData::getTrainingDayId , p.getTrainingDayId())
                                    .eq(RaceData::getGroupId , p.getGroupId()));

                    // 其它心率
                    String otherHeartRate = "";
                    // 其它乳酸
                    String otherLacticAcid = "";
                    if(raceDataList.size() > 0){
                        for (int i = 0; i<raceDataList.size(); i++){
                            // 判断其它心率
                            System.out.println();
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getHeartRate())){
                                otherHeartRate += raceDataList.get(i).getHeartRateTitle()+":"+raceDataList.get(i).getHeartRate() + " ";
                            }
                            // 判断其它乳酸
                            if(StringUtils.isNotEmpty(raceDataList.get(i).getLacticAcid())){
                                otherLacticAcid += raceDataList.get(i).getLacticAcidTitle()+":"+raceDataList.get(i).getLacticAcid() + " ";
                            }
                        }
                    }
                    // 存入其它心率
                    if(otherHeartRate.length() > 0){
                        String otherHeartRates = otherHeartRate.substring(0 , otherHeartRate.length()-1);
                        p.setOtherHeartRate(otherHeartRates);
                    }
                    // 存入其它乳酸
                    if(otherLacticAcid.length() > 0){
                        String otherLacticAcids = otherLacticAcid.substring(0 , otherLacticAcid.length()-1);
                        p.setOtherLacticAcid(otherLacticAcids);
                    }



                    /**
                     * 查询水上分段数据
                     */
                    List<HeartRateCount> heartRateCountList = heartRateCountService
                            .list(Wrappers.<HeartRateCount>lambdaQuery()
                                    .eq(HeartRateCount::getGroupId , p.getGroupId())
                                    .eq(HeartRateCount::getChannelId , p.getChannelId())
                                    .eq(HeartRateCount::getTrainingDayId , p.getTrainingDayId()));
                    // 设置分段速度
                    String sectionalSpeed = "";
                    for (int s = 0; s<heartRateCountList.size(); s++){
                        if(StringUtils.isNotNull(heartRateCountList.get(s).getShipSpeed()) && heartRateCountList.get(s).getShipSpeed()>0.0){
                            sectionalSpeed+=heartRateCountList.get(s).getShipSpeed()+" ";
                        }
                    }
                    // 存储分段速度
                    if(StringUtils.isNotNull(sectionalSpeed) && !"".equals(sectionalSpeed)){
                        p.setSectionalSpeed(sectionalSpeed.substring(0 , sectionalSpeed.length()-1));
                    }

                    // 设置平均船速
                    Double sectionalSpeeds = 0.0;
                    for (int s = 0; s<heartRateCountList.size(); s++){
                        if(StringUtils.isNotNull(heartRateCountList.get(s).getShipSpeed()) && heartRateCountList.get(s).getShipSpeed()>0.0){
                            sectionalSpeeds+=heartRateCountList.get(s).getShipSpeed();
                        }
                    }
                    if(sectionalSpeeds>0.0){
                        Double sectionalSpeeds2 = sectionalSpeeds/heartRateCountList.size();
                        // 平均船速
                        p.setAverageSpeed(String.format("%.2f", sectionalSpeeds2));
                    }

                    // 设置分段桨频
                    String segmentPropellerFrequency = "";
                    for (int s = 0; s<heartRateCountList.size(); s++){
                        if(StringUtils.isNotNull(heartRateCountList.get(s).getPropellerFrequency()) && !"".equals(heartRateCountList.get(s).getPropellerFrequency())){
                            segmentPropellerFrequency+=heartRateCountList.get(s).getPropellerFrequency()+" ";
                        }
                    }
                    if(!"".equals(segmentPropellerFrequency)){
                        p.setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0 , segmentPropellerFrequency.length()-1));
                    }

                    // 设置平均桨频
                    Double averageFrequency = 0.0;
                    for (int s = 0; s<heartRateCountList.size(); s++){
                        if(StringUtils.isNotNull(heartRateCountList.get(s).getPropellerFrequency()) && !"".equals(heartRateCountList.get(s).getPropellerFrequency())){
                            averageFrequency+=Double.parseDouble(heartRateCountList.get(s).getPropellerFrequency());
                        }
                    }
                    // 存储 平均桨频
                    if(StringUtils.isNotNull(averageFrequency) && averageFrequency>0.0){
                        Double averagePropellerFrequency = averageFrequency.doubleValue()/heartRateCountList.size();
                        p.setAveragePropellerFrequency(averagePropellerFrequency);
                    }

                    // 查询水上心率表 id
                    List<Integer> rateCountId = new ArrayList<>();
                    for (int n = 0; n<heartRateCountList.size(); n++){
                        rateCountId.add(heartRateCountList.get(n).getId());
                    }


                    for (int n = 0; n<heartRateCountList.size(); n++){
                        // 平均心率
                        Integer averageHeartRate = 0;
                        Integer recordList = 0;
                        // 分段心率
                        String segmentHeartRate = "";

                        List<HeartRateRecord> heartRateRecordList = heartRateRecordService
                                .list(Wrappers.<HeartRateRecord>lambdaQuery()
                                        .in(HeartRateRecord::getHeartRateCountId , rateCountId)
                                        .eq(HeartRateRecord::getSportsmanId , p.getSportsmanId()));
                        recordList = heartRateRecordList.size();
                        for (int i = 0; i<heartRateRecordList.size(); i++){
                            if(StringUtils.isNotNull(heartRateRecordList.get(i).getHeartRateAvg())){
                                segmentHeartRate+=heartRateRecordList.get(i).getHeartRateAvg()+" ";
                                averageHeartRate+=heartRateRecordList.get(i).getHeartRateAvg();
                            }
                        }
                        // 设置分段心率
                        if(!"".equals(segmentHeartRate)){
                            p.setSegmentHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                        }
                        // 计算平均心率
                        if(averageHeartRate>0){
                            p.setAverageHeartRate(averageHeartRate/recordList);
                        }
                    }



                    if(p.getTrainingMode() == 1){
                        // 重新赋值总成绩
                        // 计算总成绩
                        Double totalDistance = 0.0;
                        for (int s = 0; s<heartRateCountList.size(); s++){
                            if(StringUtils.isNotNull(heartRateCountList.get(s).getTotalDistance()) && heartRateCountList.get(s).getTotalDistance()>0.0){
                                totalDistance+=heartRateCountList.get(s).getTotalDistance();
                            }
                        }
                        if(totalDistance>0.0){
                            p.setAchievement(totalDistance.toString());
                        }

                        // 重新赋值分段成绩
                        String segmentedTiming = "";
                        for (int s = 0; s<heartRateCountList.size(); s++){
                            if(StringUtils.isNotNull(heartRateCountList.get(s).getTotalDistance()) && heartRateCountList.get(s).getTotalDistance()>0.0){
                                segmentedTiming+=heartRateCountList.get(s).getTotalDistance() + " ";
                            }
                        }
                        if(!"".equals(segmentedTiming)){
                            p.setSegmentedTiming(segmentedTiming.substring(0 , segmentedTiming.length()-1));
                        }
                    }




                }
            });
        }

        return getDataTable(list);
    }

    /**
     * 导出训练成绩列表
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:export')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TrainingAchievement trainingAchievement) {
        List<AchievementExcel> list = trainingAchievementService.selectTrainingAchievementListExcel(trainingAchievement);
        AjaxResult ajaxResult;

        // 查询所有运动员
        List<Sportsman> sportsmanList = sportsmanService.list();

        for (int i = 0; i<list.size(); i++) {
            // 查询课程
            TrainingDay trainingDay = trainingDayService.getById(list.get(i).getTrainingDayId());
            if(StringUtils.isNotNull(trainingDay)){
                list.get(i).setRowingClassHour((int)trainingDay.getClassHour());
            }

            // 设置水上陆上中文名称
            if (list.get(i).getType().equals("land_specific_training_dynamometer")) {

                if(StringUtils.isNull(list.get(i).getPeakForce())){
                    list.get(i).setPeakForce(0.0);
                }else{
                    list.get(i).setPeakForce(Double.parseDouble(String.format("%.2f", list.get(i).getPeakForce())));
                }

                // 陆上训练
                list.get(i).setFirstLevelName("陆上专项训练");
                // 根据 运动员的 id 查询运动员的名称 并且放到返回值中
                Sportsman sportsman = sportsmanService.getById(list.get(i).getSportsmanId());
                if(StringUtils.isNotNull(sportsman)){
                    list.get(i).setSportsmanName(sportsman.getName());
                }

                // 存入速度
                if(StringUtils.isNotNull(list.get(i).getSpeed()) && !"".equals(list.get(i).getSpeed())){
                    list.get(i).setAverageSpeed(list.get(i).getSpeed().toString());
                }


                // 查询分段成绩数据
                // 查询分段成绩
                List<DynamometerSubsection> dynamometerSubsectionList = dynamometerSubsectionService
                        .list(Wrappers.<DynamometerSubsection>lambdaQuery()
                                .eq(DynamometerSubsection::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(DynamometerSubsection::getGroupId, list.get(i).getGroupId()));

                // 平均功率
                Integer averagePower = 0;
                // 平均桨频
                Double averagePropellerFrequency = 0.0;
                // 分段成绩
                String averageAchievement = "";
                // 分段成绩下标
                Integer averageAchievementIndex = 0;
                // 分段功率
                String segmentPower = "";
                // 分段功率下标
                Integer segmentPowerIndex = 0;
                // 速度
                double averageSpeed = 0;
                // 分段桨频
                String segmentPropellerFrequency = "";
                // 分段桨频下标
                Integer segmentPropellerFrequencyIndex = 0;
                // 分段心率
                String segmentHeartRate = "";
                // 分段心率下标
                Integer segmentHeartRateIndex = 0;
                // 分段瓦特
                String segmentWatt = "";
                // 分段瓦特下标
                Integer segmentWattIndex = 0;
                // 平均心率
                Integer averageHeartRate = 0;

                // 分段总数
                Integer dynamometerSubsectionSize = dynamometerSubsectionList.size();

                for (int j = 0; j < dynamometerSubsectionList.size(); j++) {
                    averagePower += (int)StringUtils.turnSecond(dynamometerSubsectionList.get(j).getPower());
                    // 存储平均功率
                    Integer averagePowers = averagePower / dynamometerSubsectionSize;
                    list.get(i).setAveragePower(StringUtils.millisecondRotationTime(averagePowers));
                    // 计算平均桨频
                    averagePropellerFrequency += Integer.parseInt(dynamometerSubsectionList.get(j).getPropellerFrequency());
                    Double averagePropellerFrequencys = averagePropellerFrequency / dynamometerSubsectionSize;
                    list.get(i).setAveragePropellerFrequency(StringUtils.numberDigits(averagePropellerFrequencys , 1));
                    // 平均瓦特
                    list.get(i).setAverageWatt(Integer.parseInt(dynamometerSubsectionList.get(0).getWatt()));
                    if (dynamometerSubsectionList.get(j).getTrainingMode() == 0) {
                        averageAchievementIndex++;
                        averageAchievement += "分段成绩"+averageAchievementIndex+": "+dynamometerSubsectionList.get(j).getLengthTime() + " " + String.valueOf((char)10);

                    } else {
                        averageAchievementIndex++;
                        averageAchievement += "分段成绩"+averageAchievementIndex+": "+dynamometerSubsectionList.get(j).getMileage()+"米" + " " + String.valueOf((char)10);
                    }
                    // 存储分段成绩
                    String segmentedTiming = averageAchievement.substring(0, averageAchievement.length() - 1);
                    list.get(i).setSegmentedTiming(segmentedTiming);
                    // 分段功率
                    segmentPowerIndex++;
                    segmentPower += "分段功率"+segmentPowerIndex+": "+dynamometerSubsectionList.get(j).getPower() + " " + String.valueOf((char)10);
                    // 存入分段功率
                    list.get(i).setSegmentPower(segmentPower.substring(0, segmentPower.length() - 1));
                    // 分段桨频
                    segmentPropellerFrequencyIndex++;
                    segmentPropellerFrequency += "分段桨频"+segmentPropellerFrequencyIndex+": "+dynamometerSubsectionList.get(j).getPropellerFrequency() + " " + String.valueOf((char)10);
                    // 存储分段桨频
                    list.get(i).setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0, segmentPropellerFrequency.length() - 1));
                    // 分段心率
                    segmentHeartRateIndex++;
                    segmentHeartRate += "分段心率"+segmentHeartRateIndex+": "+dynamometerSubsectionList.get(j).getHeartRate() + " " + String.valueOf((char)10);
                    // 存储分段心率
                    list.get(i).setSegmentHeartRate(segmentHeartRate.substring(0, segmentHeartRate.length() - 1));
                    // 分段瓦特
                    segmentWattIndex++;
                    segmentWatt += "分段瓦特"+segmentWattIndex+": "+dynamometerSubsectionList.get(j).getWatt() + " " + String.valueOf((char)10);
                    // 存储分段瓦特
                    list.get(i).setSegmentWatt(segmentWatt.substring(0, segmentWatt.length() - 1));
                    // 计算平均心率
                    if (StringUtils.isNotNull(dynamometerSubsectionList.get(j).getHeartRate())) {
                        averageHeartRate += dynamometerSubsectionList.get(j).getHeartRate();
                    }
                    // 存储平均心率
                    list.get(i).setAverageHeartRate(averageHeartRate / dynamometerSubsectionSize);


                }


                /**
                 * 查询最大心率
                 */
                List<Dynamometer> dynamometerList = dynamometerService
                        .list(Wrappers.<Dynamometer>lambdaQuery()
                                .eq(Dynamometer::getSportsmanId, list.get(i).getSportsmanId())
                                .eq(Dynamometer::getGroupId, list.get(i).getGroupId())
                                .eq(Dynamometer::getTrainingMode, list.get(i).getTrainingMode())
                                .orderByDesc(Dynamometer::getHeartRate));
                if (dynamometerList.size() > 0) {
                    list.get(i).setMaxHeartRate(dynamometerList.get(0).getHeartRate());
                }

                // 查询其它心率
                List<RaceData> raceDataList2 = raceDataService
                        .list(Wrappers.<RaceData>lambdaQuery()
                                .eq(RaceData::getSportsmanId , list.get(i).getSportsmanId())
                                .eq(RaceData::getGroupId , list.get(i).getGroupId()));

                // 其它心率
                String otherHeartRate = "";
                // 其它乳酸
                String otherLacticAcid = "";
                if (raceDataList2.size() > 0) {
                    for (int g = 0; g < raceDataList2.size(); g++) {
                        if(list.get(i).getSportsmanId() == raceDataList2.get(g).getSportsmanId()
                                && list.get(i).getTrainingDayId()
                                .equals(raceDataList2.get(g).getTrainingDayId())){
                            // 判断其它心率
                            if (StringUtils.isNotEmpty(raceDataList2.get(g).getHeartRate())) {
                                otherHeartRate += raceDataList2.get(g).getHeartRateTitle() + ":" + raceDataList2.get(g).getHeartRate() + " " + String.valueOf((char)10);
                            }
                            // 判断其它乳酸
                            if (StringUtils.isNotEmpty(raceDataList2.get(g).getLacticAcid())) {
                                otherLacticAcid += raceDataList2.get(g).getLacticAcidTitle() + ":" + raceDataList2.get(g).getLacticAcid() + " " + String.valueOf((char)10);
                            }
                        }
                    }
                }
                // 存入其它心率
                if (otherHeartRate.length() > 0) {
                    String otherHeartRates = otherHeartRate.substring(0, otherHeartRate.length() - 1);
                    list.get(i).setOtherHeartRate(otherHeartRates);
                }
                // 存入其它乳酸
                if (otherLacticAcid.length() > 0) {
                    String otherLacticAcids = otherLacticAcid.substring(0, otherLacticAcid.length() - 1);
                    list.get(i).setOtherLacticAcid(otherLacticAcids);
                }

            } else {
                // 水上训练
                list.get(i).setFirstLevelName("水上专项训练");
                // 设置航道名称
                if(!"".equals(list.get(i).getChannelName()) && StringUtils.isNotNull(list.get(i).getChannelName())){
                    list.get(i).setChannelName("第" + Integer.parseInt(list.get(i).getChannelName() + 1) + "航道");
                }

                // 设置人的名称
                    // 根据 运动员的 id 查询运动员的名称 并且放到返回值中
                        // 运动员名称
                        String sportsmanName = "";
                        for (int g = 0; g < sportsmanList.size(); g++) {
                            if(list.get(i).getSportsmanId() == (sportsmanList.get(g).getId())){
                                sportsmanName += sportsmanList.get(g).getName() + " ";
                            }
                        }
                        list.get(i).setSportsmanName(sportsmanName);


                // 判断左桨，还是又桨
                if(list.get(i).getOarType() == 0){
                    list.get(i).setOarTypeName("双桨");
                }else if(list.get(i).getOarType() == 1){
                    list.get(i).setOarTypeName("左桨");
                }else if(list.get(i).getOarType() == 2){
                    list.get(i).setOarTypeName("右桨");
                }


                // 查询其它心率
                List<RaceData> raceDataList = raceDataService
                        .list(Wrappers.<RaceData>lambdaQuery()
                                .eq(RaceData::getSportsmanId , list.get(i).getSportsmanId())
                                .eq(RaceData::getTrainingDayId , list.get(i).getTrainingDayId())
                                .eq(RaceData::getGroupId , list.get(i).getGroupId()));

                // 其它心率
                String otherHeartRate = "";
                // 其它乳酸
                String otherLacticAcid = "";
                if (raceDataList.size() > 0) {
                    for (int g = 0; g < raceDataList.size(); g++) {
                        if(list.get(i).getSportsmanId() == raceDataList.get(g).getSportsmanId()
                                && list.get(i).getTrainingDayId()
                                .equals(raceDataList.get(g).getTrainingDayId())){
                            // 判断其它心率
                            if (StringUtils.isNotEmpty(raceDataList.get(g).getHeartRate())) {
                                otherHeartRate += raceDataList.get(g).getHeartRateTitle() + ":" + raceDataList.get(g).getHeartRate() + " " + String.valueOf((char)10);
                            }
                            // 判断其它乳酸
                            if (StringUtils.isNotEmpty(raceDataList.get(g).getLacticAcid())) {
                                otherLacticAcid += raceDataList.get(g).getLacticAcidTitle() + ":" + raceDataList.get(g).getLacticAcid() + " " + String.valueOf((char)10);
                            }
                        }
                        }
                }
                // 存入其它心率
                if (otherHeartRate.length() > 0) {
                    String otherHeartRates = otherHeartRate.substring(0, otherHeartRate.length() - 1);
                    list.get(i).setOtherHeartRate(otherHeartRates);
                }
                // 存入其它乳酸
                if (otherLacticAcid.length() > 0) {
                    String otherLacticAcids = otherLacticAcid.substring(0, otherLacticAcid.length() - 1);
                    list.get(i).setOtherLacticAcid(otherLacticAcids);
                }

                // 添加小组
                if(StringUtils.isNotNull(list.get(i).getBatch())){
                    Integer batch = list.get(i).getBatch()+1;
                    list.get(i).setBatchName("小组" + batch);
                }


                // 分段心率
                list.get(i).setHeartRate("");

                // 分段船速
                /**
                 * 查询水上分段数据
                 */
                List<HeartRateCount> heartRateCountList = heartRateCountService
                        .list(Wrappers.<HeartRateCount>lambdaQuery()
                                .eq(HeartRateCount::getGroupId , list.get(i).getGroupId())
                                .eq(HeartRateCount::getChannelId , list.get(i).getChannelId()));
                // 设置分段速度
                String sectionalSpeed = "";
                // 分段速度下标
                int indexSpeed = 1;
                for (int s = 0; s<heartRateCountList.size(); s++){
                    if(StringUtils.isNotNull(heartRateCountList.get(s).getShipSpeed()) && heartRateCountList.get(s).getShipSpeed()>0.0){
                        sectionalSpeed += "分段船速" + indexSpeed + "：" + heartRateCountList.get(s).getShipSpeed() + " " + String.valueOf((char)10);
                        indexSpeed ++;
                    }
                }
                // 存储分段速度
                if(StringUtils.isNotNull(sectionalSpeed) && !"".equals(sectionalSpeed)){
                    list.get(i).setSectionalSpeed(sectionalSpeed.substring(0 , sectionalSpeed.length()-1));
                }

                // 设置平均船速
                Double sectionalSpeeds = 0.0;
                for (int s = 0; s<heartRateCountList.size(); s++){
                    if(StringUtils.isNotNull(heartRateCountList.get(s).getShipSpeed()) && heartRateCountList.get(s).getShipSpeed()>0.0){
                        sectionalSpeeds+=heartRateCountList.get(s).getShipSpeed();
                    }
                }
                if(sectionalSpeeds > 0.0){
                    Double sectionalSpeeds2 = sectionalSpeeds/heartRateCountList.size();
                    // 平均船速
                    list.get(i).setAverageSpeed(String.format("%.2f", sectionalSpeeds2));
                }

                // 设置分段桨频
                String segmentPropellerFrequency = "";
                // 分段桨频下标
                int indexFrequency = 1;
                for (int s = 0; s<heartRateCountList.size(); s++){
                    if(StringUtils.isNotNull(heartRateCountList.get(s).getPropellerFrequency()) && !"".equals(heartRateCountList.get(s).getPropellerFrequency())){
                        segmentPropellerFrequency += "分段桨频" + indexFrequency + "：" + heartRateCountList.get(s).getPropellerFrequency() + " " + String.valueOf((char)10);
                        indexFrequency ++;
                    }
                }
                if(!"".equals(segmentPropellerFrequency)){
                    list.get(i).setPropellerFrequency(segmentPropellerFrequency.substring(0 , segmentPropellerFrequency.length()-1));
                }

                // 设置平均桨频
                Double averageFrequency = 0.0;
                // 分段成绩
                String segmentedTiming = "";
                // 分段下标，从 1 开始
                int indexTiming = 1;
                for (int s = 0; s<heartRateCountList.size(); s++){
                    if(StringUtils.isNotNull(heartRateCountList.get(s).getPropellerFrequency()) && !"".equals(heartRateCountList.get(s).getPropellerFrequency())){
                        averageFrequency+=Double.parseDouble(heartRateCountList.get(s).getPropellerFrequency());
                    }

                    // 重新设置分段成绩
                    if(StringUtils.isNotNull(heartRateCountList.get(s).getPointAchievement()) && !"".equals(heartRateCountList.get(s).getPointAchievement())){
                        segmentedTiming += "分段成绩"+ indexTiming + "：" + heartRateCountList.get(s).getPointAchievement() + " " + String.valueOf((char)10);
                    }
                    indexTiming ++;
                }
                // 重新设置分段成绩
                if(StringUtils.isNotNull(segmentedTiming) && !"".equals(segmentedTiming)){
                    list.get(i).setSegmentedTiming(segmentedTiming);
                }

                // 存储 平均桨频
                if(StringUtils.isNotNull(averageFrequency) && averageFrequency>0.0){
                    Double averagePropellerFrequencys = averageFrequency.doubleValue()/heartRateCountList.size();
                    list.get(i).setAveragePropellerFrequency(StringUtils.numberDigits(averagePropellerFrequencys , 1));
                }


                // 分段心率
                String segmentHeartRate = "";
                for (int m = 0; m<heartRateCountList.size(); m++){
                    // 分段心率下标
                    int indexHeartRate = 1;
                    // 平均心率
                    Integer averageHeartRate = 0;
                    List<HeartRateRecord> heartRateRecordList = heartRateRecordService
                            .list(Wrappers.<HeartRateRecord>lambdaQuery()
                                    .eq(HeartRateRecord::getHeartRateCountId , heartRateCountList.get(m).getId()));
                    for (int k = 0; k<heartRateRecordList.size(); k++){
                        if(StringUtils.isNotNull(heartRateRecordList.get(k).getHeartRateAvg())){
                            segmentHeartRate+= "分段心率" + indexHeartRate + "：" + heartRateRecordList.get(k).getHeartRateAvg() + " " + String.valueOf((char)10);
                            indexHeartRate ++;
                            averageHeartRate+=heartRateRecordList.get(k).getHeartRateAvg();
                        }
                    }
                    if(averageHeartRate>0){
                        list.get(i).setAverageHeartRate(averageHeartRate/heartRateRecordList.size());
                    }
                }
                // 设置分段心率
                if(!"".equals(segmentHeartRate)){
                    list.get(i).setHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                }




            }
        }


        if(trainingAchievement.getFirstLevel().equals("special_training_water")){
            List<AchievementExcel.AchievementWater> achievementWaters = new ArrayList<>();
            for(AchievementExcel achievementExcel : list){
                achievementWaters.add(achievementExcel.getAchievementWater(achievementExcel));
            }
            ExcelUtil<AchievementExcel.AchievementWater> util = new ExcelUtil<>(AchievementExcel.AchievementWater.class);
            ajaxResult = util.exportExcel(achievementWaters, "训练成绩");
        }else{
            List<AchievementExcel.AchievementLand> achievementLands = new ArrayList<>();
            for(AchievementExcel achievementExcel : list){
                achievementLands.add(achievementExcel.getAchievementLand(achievementExcel));
            }
            ExcelUtil<AchievementExcel.AchievementLand> util = new ExcelUtil<>(AchievementExcel.AchievementLand.class);
            ajaxResult = util.exportExcel(achievementLands, "训练成绩");
        }
        return ajaxResult;
    }

    /**
     * 获取训练成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingAchievementService.getById(id));
    }

    /**
     * 新增训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:add')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievement.setCtime(new Date());
        trainingAchievementService.save(trainingAchievement);
        return AjaxResult.success();
    }

    /**
     * 修改训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:edit')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievementService.updateById(trainingAchievement);
        return AjaxResult.success();
    }

    /**
     * 删除训练成绩
     */
    @PreAuthorize("@ss.hasPermi('trainingAchievement:trainingAchievement:remove')")
    @Log(title = "TrainingAchievement", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        trainingAchievementService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    
}